package com.pku.jtestx86.command.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jdt.internal.core.CompilationUnit;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.handlers.HandlerUtil;

import com.pku.jtestx86.views.CFGView;

public class CFGHandler extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		// TODO Auto-generated method stub
		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
		TreeSelection selection=(TreeSelection)window.getSelectionService().getSelection();
		
		int segmentCount=selection.getPaths()[0].getSegmentCount();
		CompilationUnit compilationUnit=(CompilationUnit) selection.getPaths()[0].getSegment(segmentCount-1);
		CFGView viewPart;
		try {
			viewPart = (CFGView) window.getActivePage().showView("com.pku.jtestx86.views.ASTView");
			viewPart.show(compilationUnit);
		} catch (PartInitException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
